#-*- encoding:utf-8 -*-
from django.db import models
from django.contrib.auth.models import User


class Server(models.Model):
    """Jenkins 服务器"""

    name = models.CharField(max_length=100, verbose_name="Server机器名")
    ip = models.IPAddressField(verbose_name="server ip")
    username = models.CharField(max_length=100, verbose_name="Jenkins管理员账号")
    password = models.CharField(max_length=100, verbose_name="Jenkins管理员账号密码")
    url = models.CharField(max_length=255, verbose_name="Url地址")
    depart = models.CharField(max_length=10, verbose_name="服务器所属部门")
    is_active = models.BooleanField(default=True, verbose_name="服务器状态")

    class Meta:
        db_table = "ci_server"


class FtpServer(models.Model):
    """存放版本的Ftp地址"""

    ip = models.IPAddressField(verbose_name="Ftp server ip")
    username = models.CharField(max_length=100, verbose_name="ftp 账号")
    password = models.CharField(max_length=100, verbose_name="ftp账号密码")
    is_active = models.BooleanField(default=True, verbose_name="状态")

    class Meta:
        db_table = "ci_ftp"


class Job(models.Model):
    """Jenkins 中的任务"""

    name = models.CharField(max_length=100, verbose_name="任务名称")
    url = models.CharField(max_length=255, verbose_name="任务url地址")
    ftp = models.ForeignKey(FtpServer, blank=True, null=True)
    ftp_path = models.CharField(max_length=255, blank=True, verbose_name="ftp的地址")
    is_active = models.BooleanField(default=True, verbose_name="状态")
    server = models.ForeignKey(Server, verbose_name="任务所在服务器")

    class Meta:
        db_table = "ci_job"


class JobBuildHistory(models.Model):
    """任务执行历史记录"""

    job = models.ForeignKey(Job, verbose_name="任务")
    build_number = models.IntegerField(max_length=3, blank=True, null=True, verbose_name='构建序号')
    manifest_url = models.CharField(max_length=350, blank=True, verbose_name='任务代码提货单')
    create_tag = models.BooleanField(default=False, verbose_name='是否创建TAG')
    tag_name = models.CharField(max_length=300, verbose_name='tag名称')
    inside_version_name = models.CharField(max_length=200, blank=True, verbose_name='软件内部版本号')
    outside_version_name = models.CharField(max_length=200, blank=True, verbose_name='软件外部版本号')
    build_category = models.CharField(max_length=50, verbose_name='构建类型')
    compile_ota = models.BooleanField(default=False, verbose_name='是否编译OTA')
    clear_workspace = models.BooleanField(default=True, verbose_name='清理工作区')
    update_code = models.BooleanField(default=True, verbose_name='同步代码')
    build_command = models.CharField(max_length=300, blank=True, verbose_name='编译命令')
    ota_command = models.CharField(max_length=300, blank=True, verbose_name='ota编译命令')
    build_configure = models.CharField(max_length=3000, blank=True, verbose_name='编译的所有配置')
    make_configure = models.CharField(max_length=2000, blank=True, verbose_name='修改项目配置项')
    package_files = models.CharField(max_length=2000, blank=True, verbose_name='打包的文件清单')
    version_description = models.CharField(max_length=255, blank=True, verbose_name='版本描述')
    build_time = models.DateTimeField(blank=True, null=True, verbose_name='启动构建时间')
    build_user = models.ForeignKey(User, verbose_name='启动构建的人')
    build_result = models.BooleanField(default=False, verbose_name='构建结果')

    manifest_context = models.TextField(verbose_name='编译时当前货单的状态')
    is_ftp = models.BooleanField(verbose_name='是否上传FTP')
    build_console = models.TextField(verbose_name='构建日志信息')

    class Meta:
        db_table = "ci_build_history"



